const { createProtobufRpcClient, QueryClient } = require("@cosmjs/stargate");
const { Tendermint34Client }  = require("@cosmjs/tendermint-rpc");

const { QueryClientImpl, QueryGetUsedNonceRequest }  = require("./circle/cctp/v1/query");
class NobleClient{
    constructor() {
        this.queryService = null;
    }
    async init(rpc) {
        try{
            const tendermintClient = await Tendermint34Client.connect(rpc);

// The generic Stargate query client knows how to use the Tendermint client to submit unverified ABCI queries
            const queryClient = new QueryClient(tendermintClient);

// This helper function wraps the generic Stargate query client for use by the specific generated query client
            const rpcClient = createProtobufRpcClient(queryClient);

// Here we instantiate a specific query client which will have the custom methods defined in the .proto file
            this.queryService = new QueryClientImpl(rpcClient);
        }catch (e) {
            console.log('NobleClient.init: e', e);
            throw e;
        }

    }

    async UsedNonce(sourceDomain, nonce) {
        if(this.queryService) {
            try{

                let ret = await this.queryService.UsedNonce(
                    {
                        sourceDomain:sourceDomain,nonce:nonce
                    }
                )
                return ret;


            }catch (e) {
                console.log('NobleClient.UsedNonce: e', e);
                return null;
            }
        }else{
            return null;
        }
    }
}
module.exports = NobleClient;